<template>
  <div class="w3l-signinform"
       :style="{
       backgroundImage:'url(' +(require('./image/bg.png')) +')',
       backgroundSize: '100% 100%',backgroundRepeat: 'no-repeat'}">
    <div class="wrapper">
      <div class="w3l-form-info">
        <div class="w3_info">
          <h2 style="text-align: center">沈家门街道三项交易审批监督管理系统</h2>
          <el-card shadow="always" class="card">
            <el-form class="login-form" status-icon :rules="loginRules"
                     ref="loginForm" :model="loginForm" label-width="0" size="default">
              <el-form-item prop="userName">
                <el-input size="small" @keyup.enter.native="handleLogin()" v-model="loginForm.userName"
                          clearable auto-complete="off" placeholder="请输入账号">
                  <i slot="prefix" class="el-icon-user"/>
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input size="small" @keyup.enter.native="handleLogin()" v-model="loginForm.password"
                          clearable show-password auto-complete="off" placeholder="请输入密码">
                  <i slot="prefix" class="el-icon-unlock"/>
                </el-input>
              </el-form-item>
              <el-form-item prop="code">
                <el-row :span="34">
                  <el-col :span="14">
                    <el-input size="small" @keyup.enter.native="handleLogin()" v-model="loginForm.code"
                              clearable auto-complete="off" placeholder="请输入验证码">
                      <i slot="prefix" class="el-icon-mobile"/>
                    </el-input>
                  </el-col>
                  <el-col :span="10">
                    <div class="login-code">
                      <img :src="codeUrl" class="login-code-img" @click="changePic" alt=""/>
                    </div>
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  :loading="loading"
                  @click.native.prevent="handleLogin"
                  class="btn-primary">登 录
                </el-button>
              </el-form-item>
            </el-form>
          </el-card>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { getInfo, doGet } from '@/api/index'

  export default {
    name: 'login',
    components: {
    },
    data() {
      return {
        loginForm: {
          userName: '',
          password: '',
          code: '',
        },
        loginRules: {
          userName: [
            { required: true, message: '请输入账号', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请输入验证码', trigger: 'blur' },
            { min: 4, max: 4, message: '验证码长度为4位', trigger: 'blur' }
          ]
        },
        codeUrl: '',
        loading: false
      }
    },
    created() {
      this.changePic()
    },
    methods: {
      changePic() {
        doGet('validateCode/getCaptchaBase64').then(res => {
          if (res.code == 200) {
            this.codeUrl = 'data:image/png;base64,' + res.data
          }
        }).catch(error => {
          this.$message.error(error.message)
        })
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('Login', this.loginForm).then(res => {
              if (res.code == 200) {
                this.$router.push({ path: this.redirect || '/' })
              } else {
                this.$message.error(res.message)
              }
              this.loading = false
            }).catch(response => {
              this.loading = false
              this.changePic()
              this.$message.error(response.message)
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import "css/style.scss";

</style>
